// KMLWriter.cpp: define el punto de entrada de la aplicación de consola.
//
#include "lib/tinyxml.h"
#include "lib/tinystr.h"

/*
	Lista de parámetros:
	1.- Número de cámara
	2.- Número de automóviles
*/
int main(int argc, char* argv[])
{
	if (argc != 3)
    {
		printf("Error: hay que enviar como primer parámetro el número de cámara y como segundo el número de automóviles");
        return -1;
    }

	TiXmlDocument doc;
	TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "UTF-8", "" );
	doc.LinkEndChild( decl );


	TiXmlElement * kml = new TiXmlElement( "kml" );
	doc.LinkEndChild( kml );

	kml->SetAttribute("xmlns", "http://www.opengis.net/kml/2.2" );
	kml->SetAttribute("xmlns:gx", "http://www.google.com/kml/ext/2.2" );
	kml->SetAttribute("xmlns:kml", "http://www.opengis.net/kml/2.2" );
	kml->SetAttribute("xmlns:atom", "http://www.w3.org/2005/Atom" );


	TiXmlElement * document = new TiXmlElement( "Document" );
	kml->LinkEndChild( document );

	TiXmlElement * name = new TiXmlElement( "name" );
	document->LinkEndChild( name );

	TiXmlText * text;

	switch (atoi(argv[1])) {
		case 1:
			text = new TiXmlText( "A1 - PK 30,800 Urbaniz. Valdelagua (San Agustín de Guadalix). ");
			break;
		case 2:
			text = new TiXmlText( "A1 - PK 34,005 Enlace M-104 (S.A. de Guadalix a Colmenar Viejo)" );
			break;
		case 3:
			text = new TiXmlText( "A1 - PK 36,620 Acceso Norte a San Agustín de Guadalix" );
			break;
		case 4:
			text = new TiXmlText( "A1 - PK 37,900 San Agustín de Guadalix (Zona Norte)" );
			break;
		case 5:
			text = new TiXmlText( "A1 - PK 43,300 Urbaniz. Vistasierra (Acceso a Pedrezuela)" );
			break;
		case 6:
			text = new TiXmlText( "A1 - PK 45,600 Pedrezuela" );
			break;
	}

	name->LinkEndChild( text );

   // bloque Style dentro de Document

		TiXmlElement * style = new TiXmlElement( "Style" );
		document->LinkEndChild( style );

		style->SetAttribute("id", "sn_cabs" );


   TiXmlElement * iconStyle = new TiXmlElement( "IconStyle" );
   style->LinkEndChild( iconStyle );

   TiXmlElement * colorIcon = new TiXmlElement( "color" );
   iconStyle->LinkEndChild( colorIcon );

	if( atoi(argv[2]) < 4 )
		text = new TiXmlText("ff0603ff");
	else if (atoi(argv[2]) >=4 || atoi(argv[2])< 8)
		text = new TiXmlText("ff0603ff");
	else if (atoi(argv[2]) >=8)
		text = new TiXmlText("ff0603ff");

	colorIcon->LinkEndChild( text );

   TiXmlElement * scale = new TiXmlElement( "scale" );
   iconStyle->LinkEndChild( scale );

   text = new TiXmlText( "1.2" );
	scale->LinkEndChild( text );

   TiXmlElement * icon = new TiXmlElement( "Icon" );
   iconStyle->LinkEndChild( icon );

   TiXmlElement * href = new TiXmlElement( "href" );
   icon->LinkEndChild( href );


   if(atoi(argv[2]) < 4 )
	   text = new TiXmlText( "http://www.trafficvision.net/img/cabGreen.png" );
   	else if (atoi(argv[2]) >=4 || atoi(argv[2])< 8)
   		text = new TiXmlText( "http://www.trafficvision.net/img/cabYellow.png" );
   	else if (atoi(argv[2]) >=8)
   		text = new TiXmlText( "http://www.trafficvision.net/img/cabRed.png" );


   href->LinkEndChild( text );



    // bloque hotspot
	{
		TiXmlElement * hotspot = new TiXmlElement( "hotspot" );
      iconStyle->LinkEndChild( hotspot );

      hotspot->SetAttribute("x", "0.5" );
		hotspot->SetAttribute("y", "0" );
		hotspot->SetAttribute("xunits", "fraction" );
		hotspot->SetAttribute("ynuits", "fraction" );
	}

   TiXmlElement * labelStyle = new TiXmlElement( "LabelStyle" );
   style->LinkEndChild( labelStyle );

   TiXmlElement * colorLabel = new TiXmlElement( "color" );
   labelStyle->LinkEndChild( colorLabel );

	if( atoi(argv[2]) < 4 )
		text = new TiXmlText("0000e13c");
	else if (atoi(argv[2]) >=4 || atoi(argv[2])< 8)
		text = new TiXmlText("00ef9e40");
	else if (atoi(argv[2]) >=8)
		text = new TiXmlText("ff0603ff");

	colorLabel->LinkEndChild( text );

   TiXmlElement * listStyle = new TiXmlElement( "ListStyle" );
   style->LinkEndChild( listStyle );


    // bloque Style dentro de Document

		TiXmlElement * styleMap = new TiXmlElement( "StyleMap" );
	   document->LinkEndChild( styleMap );

      styleMap->SetAttribute("id", "msn_cabs" );

   TiXmlElement * pair = new TiXmlElement( "Pair" );
   styleMap->LinkEndChild( pair );

   TiXmlElement * key = new TiXmlElement( "key" );
   pair->LinkEndChild( key );

   text = new TiXmlText( "normal" );
	key->LinkEndChild( text );

   TiXmlElement * styleUrl = new TiXmlElement( "styleUrl" );
   pair->LinkEndChild( styleUrl );

   text = new TiXmlText( "#sn_cabs" );
	styleUrl->LinkEndChild( text );

	TiXmlElement * pair2 = new TiXmlElement( "Pair" );
	   styleMap->LinkEndChild( pair2 );

	   TiXmlElement * key2 = new TiXmlElement( "key" );
	   pair2->LinkEndChild( key2 );

	   text = new TiXmlText( "highlight" );
		key2->LinkEndChild( text );

	   TiXmlElement * styleUrl2 = new TiXmlElement( "styleUrl" );
	   pair2->LinkEndChild( styleUrl2 );

	   text = new TiXmlText( "#sn_cabs0" );
		styleUrl2->LinkEndChild( text );
	TiXmlElement * placemark = new TiXmlElement( "Placemark" );
	document->LinkEndChild( placemark );

   TiXmlElement * secondName = new TiXmlElement( "name" );
	placemark->LinkEndChild( secondName );

	switch (atoi(argv[1])) {
		case 1:
			text = new TiXmlText( "A1 - PK 30,800 Urbaniz. Valdelagua (San Agustín de Guadalix). ");
			break;
		case 2:
			text = new TiXmlText( "A1 - PK 34,005 Enlace M-104 (S.A. de Guadalix a Colmenar Viejo)" );
			break;
		case 3:
			text = new TiXmlText( "A1 - PK 36,620 Acceso Norte a San Agustín de Guadalix" );
			break;
		case 4:
			text = new TiXmlText( "A1 - PK 37,900 San Agustín de Guadalix (Zona Norte)" );
			break;
		case 5:
			text = new TiXmlText( "A1 - PK 43,300 Urbaniz. Vistasierra (Acceso a Pedrezuela)" );
			break;
		case 6:
			text = new TiXmlText( "A1 - PK 45,600 Pedrezuela" );
			break;
	}

   	secondName->LinkEndChild( text );

   TiXmlElement * description = new TiXmlElement( "description" );
	placemark->LinkEndChild( description );

	if(atoi(argv[2]) < 4 )
		text = new TiXmlText( "Tráfico rápido" );
	else if (atoi(argv[2]) >=4 || atoi(argv[2])< 8)
		text = new TiXmlText( "Tráfico fluído" );
	else if (atoi(argv[2]) >=8)
		text = new TiXmlText( "Tráfico lento" );

	description->LinkEndChild( text );

    TiXmlElement * lookAt = new TiXmlElement( "LookAt" );
	placemark->LinkEndChild( lookAt );

    TiXmlElement * longitude = new TiXmlElement( "longitude" );
	lookAt->LinkEndChild( longitude );

	switch (atoi(argv[1])) {
		case 1:
			text = new TiXmlText( "-3.5966062545776367" );
			break;
		case 2:
			text = new TiXmlText( "-3.611326217651367" );
			break;
		case 3:
			text = new TiXmlText( "-3.6046314239501953" );
			break;
		case 4:
			text = new TiXmlText( "-3.5843753814697266" );
			break;
		case 5:
			text = new TiXmlText( "-3.5672736167907715" );
			break;
		case 6:
			text = new TiXmlText( "-3.577144145965576" );
			break;
	}
    longitude->LinkEndChild( text );

	TiXmlElement * latitude = new TiXmlElement( "latitude" );
	lookAt->LinkEndChild( latitude );

	switch (atoi(argv[1])) {
		case 1:
			text = new TiXmlText( "40.648508320518175" );
			break;
		case 2:
			text = new TiXmlText( "40.67315224317255" );
			break;
		case 3:
			text = new TiXmlText( "40.68805788386354" );
			break;
		case 4:
			text = new TiXmlText( "40.7039362086761" );
			break;
		case 5:
			text = new TiXmlText( "40.73525876698632" );
			break;
		case 6:
			text = new TiXmlText( "40.746541683887294" );
			break;
	}
   latitude->LinkEndChild( text );

   TiXmlElement * altitude = new TiXmlElement( "altitude" );
	lookAt->LinkEndChild( altitude );

   text = new TiXmlText( "0" );
	altitude->LinkEndChild( text );

   TiXmlElement * range = new TiXmlElement( "range" );
	lookAt->LinkEndChild( range );

   text = new TiXmlText( "252.5985624865491" );
	range->LinkEndChild( text );

   TiXmlElement * tilt = new TiXmlElement( "tilt" );
	lookAt->LinkEndChild( tilt );

   text = new TiXmlText( "41.36422996011976" );
	tilt->LinkEndChild( text );

   TiXmlElement * heading = new TiXmlElement( "heading" );
	lookAt->LinkEndChild( heading );

   text = new TiXmlText( "-0.01867887719964967" );
	heading->LinkEndChild( text );

   TiXmlElement * altitudeMode = new TiXmlElement( "altitudeMode" );
	lookAt->LinkEndChild( altitudeMode );

   text = new TiXmlText( "relativeToGround<" );
	altitudeMode->LinkEndChild( text );

   TiXmlElement * gxAltitudeMode = new TiXmlElement( "gx:altitudeMode" );
	lookAt->LinkEndChild( gxAltitudeMode );

   text = new TiXmlText( "relativeToSeaFloor" );
	gxAltitudeMode->LinkEndChild( text );

   TiXmlElement * secondStyleUrl = new TiXmlElement( "styleUrl" );
	placemark->LinkEndChild( secondStyleUrl );

   text = new TiXmlText( "#msn_cabs" );
	secondStyleUrl->LinkEndChild( text );

   TiXmlElement * point = new TiXmlElement( "Point" );
	placemark->LinkEndChild( point );

   TiXmlElement * coordinates = new TiXmlElement( "coordinates" );
	point->LinkEndChild( coordinates );

	switch (atoi(argv[1])) {
		case 1:
			text = new TiXmlText( "-3.5966062545776367,40.648508320518175,0" );
			break;
		case 2:
			text = new TiXmlText( "-3.611326217651367,40.67315224317255,0" );
			break;
		case 3:
			text = new TiXmlText( "-3.6046314239501953,40.68805788386354,0" );
			break;
		case 4:
			text = new TiXmlText( "-3.5843753814697266,40.7039362086761,0" );
			break;
		case 5:
			text = new TiXmlText( "-3.5672736167907715,40.73525876698632,0" );
			break;
		case 6:
			text = new TiXmlText( "-3.577144145965576,40.746541683887294,0" );
			break;
	}

	coordinates->LinkEndChild( text );

	switch (atoi(argv[1])) {
		case 1:
			doc.SaveFile( "A1_30800.kml" );
			break;
		case 2:
			doc.SaveFile( "A1_34005.kml" );
			break;
		case 3:
			doc.SaveFile( "A1_36620.kml" );
			break;
		case 4:
			doc.SaveFile( "A1_37900.kml" );
			break;
		case 5:
			doc.SaveFile( "A1_43300.kml" );
			break;
		case 6:
			doc.SaveFile( "A1_45600.kml" );
			break;
	}
}

